<?php

/**
 * This is the model class for table "rs_business".
 *
 * The followings are the available columns in table 'rs_business':
 * @property integer $id
 * @property integer $status
 * @property string $name
 * @property string $bio
 * @property string $website
 * @property string $type
 * @property string $overview
 * @property integer $payment_option
 * @property string $business_url
 * @property integer $logo
 * @property string $address1
 * @property string $address2
 * @property string $zip
 * @property string $state
 * @property string $city
 * @property double $lat
 * @property double $long
 * @property integer $gallery
 * @property string $create_time
 * @property string $update_time
 * @property integer $created_by
 * @property integer $updated_by
 *
 * The followings are the available model relations:
 * @property User $createdBy
 * @property User[] $rsUsers
 * @property Service[] $services
 */
class Business extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Business the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

    /**
     * @return array
     */
    public function behaviors() {
        return array(
            'EJsonBehavior'=>array(
                'class'=>'application.behaviors.EJsonBehavior'
            ),
        );
    }

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'rs_business';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name', 'required'),
			array('payment_option, logo, gallery, created_by, updated_by', 'numerical', 'integerOnly'=>true),
			array('lat, long', 'numerical'),
			array('name, website, business_url, address1, address2', 'length', 'max'=>255),
			array('type', 'length', 'max'=>128),
			array('zip', 'length', 'max'=>15),
			array('state', 'length', 'max'=>10),
			array('city', 'length', 'max'=>125),
			array('bio, overview, create_time, update_time', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, bio, website, type, overview, payment_option, business_url, logo, address1, address2, zip, state, city, lat, long, gallery, create_time, update_time, created_by, updated_by', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'createdBy' => array(self::BELONGS_TO, 'User', 'created_by'),
			/*'businessUser' => array(self::MANY_MANY, 'User', 'rs_business_user(business_id, employee_id)'),*/
            'businessUser'=>array(self::HAS_MANY,'BusinessUser','business_id'),
            'employees'=>array(
                self::HAS_MANY,'User',array('employee_id'=>'id'),'through'=>'businessUser'
            ),
			'services' => array(self::HAS_MANY, 'Service', 'business_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Name',
			'bio' => 'Bio',
			'website' => 'Website',
			'type' => 'Type',
			'overview' => 'Overview',
			'payment_option' => 'Payment Option',
			'business_url' => 'Business Url',
			'logo' => 'Logo',
			'address1' => 'Address1',
			'address2' => 'Address2',
			'zip' => 'Zip',
			'state' => 'State',
			'city' => 'City',
			'lat' => 'Lat',
			'long' => 'Long',
			'gallery' => 'Gallery',
			'create_time' => 'Create Time',
			'update_time' => 'Update Time',
			'created_by' => 'Created By',
			'updated_by' => 'Updated By',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('bio',$this->bio,true);
		$criteria->compare('website',$this->website,true);
		$criteria->compare('type',$this->type,true);
		$criteria->compare('overview',$this->overview,true);
		$criteria->compare('payment_option',$this->payment_option);
		$criteria->compare('business_url',$this->business_url,true);
		$criteria->compare('logo',$this->logo);
		$criteria->compare('address1',$this->address1,true);
		$criteria->compare('address2',$this->address2,true);
		$criteria->compare('zip',$this->zip,true);
		$criteria->compare('state',$this->state,true);
		$criteria->compare('city',$this->city,true);
		$criteria->compare('lat',$this->lat);
		$criteria->compare('long',$this->long);
		$criteria->compare('gallery',$this->gallery);
		$criteria->compare('create_time',$this->create_time,true);
		$criteria->compare('update_time',$this->update_time,true);
		$criteria->compare('created_by',$this->created_by);
		$criteria->compare('updated_by',$this->updated_by);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}